package com.ggh.jinjilive.ui.mall.goods;

import android.content.Intent;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.lifecycle.Observer;
import androidx.lifecycle.ViewModelProvider;
import androidx.recyclerview.widget.GridLayoutManager;
import androidx.recyclerview.widget.LinearLayoutManager;

import com.alibaba.android.arouter.launcher.ARouter;
import com.blankj.utilcode.util.ToastUtils;
import com.ggh.jinjilive.BaseApplication;
import com.ggh.jinjilive.R;
import com.ggh.jinjilive.databinding.FragmentGoodsBinding;
import com.ggh.jinjilive.listener.AddItemOnClickListener;
import com.ggh.jinjilive.listener.AddOnClickListener;
import com.ggh.jinjilive.mobile.mall.MallGoods;
import com.ggh.jinjilive.mobile.mall.MallPointsClass;
import com.ggh.jinjilive.pagestatus.EmptyCallback;
import com.ggh.jinjilive.pagestatus.LoadingCallback;
import com.ggh.jinjilive.ui.BaseFragment;
import com.ggh.jinjilive.ui.mall.goods.adapter.GoodsAdapter;
import com.ggh.jinjilive.ui.mall.goods.adapter.GoodsPointsAdapter;
import com.ggh.jinjilive.ui.mall.goods.affirm.GoodsAffirmOrderActivity;
import com.ggh.jinjilive.ui.mall.goods.details.GoodsDetailsActivity;
import com.ggh.jinjilive.ui.mine.personal.PersonalDataActivity;
import com.ggh.jinjilive.utils.ARouterUtils;
import com.ggh.jinjilive.views.DividerItemDecoration;
import com.ggh.jinjilive.views.drag.DrawableLayout;
import com.kingja.loadsir.callback.Callback;
import com.kingja.loadsir.core.LoadSir;
import com.scwang.smart.refresh.footer.ClassicsFooter;
import com.scwang.smart.refresh.header.ClassicsHeader;
import com.scwang.smart.refresh.header.MaterialHeader;
import com.scwang.smart.refresh.layout.api.RefreshLayout;
import com.scwang.smart.refresh.layout.listener.OnLoadMoreListener;
import com.scwang.smart.refresh.layout.listener.OnRefreshListener;

import org.jetbrains.annotations.NotNull;

import java.util.List;

public class GoodsFragment extends BaseFragment<FragmentGoodsBinding> implements OnRefreshListener, OnLoadMoreListener,View.OnClickListener{
    private GoodsViewModel goodsViewModel;
    //分类适配器
    private GoodsPointsAdapter goodsPointsAdapter;
    //商品适配器
    private GoodsAdapter goodsAdapter;
    //
    private GridLayoutManager gridLayoutManager;

    @Override
    public void onCreate(@Nullable @org.jetbrains.annotations.Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        BaseApplication.getInstance().getAppComponent().doInjectionMain(this);
        goodsViewModel = new ViewModelProvider(this,viewModelFactory).get(GoodsViewModel.class);
    }

    @Override
    public int initContentView(LayoutInflater inflater, @Nullable @org.jetbrains.annotations.Nullable ViewGroup container, @Nullable @org.jetbrains.annotations.Nullable Bundle savedInstanceState) {
        return R.layout.fragment_goods;
    }


    @Override
    public void onViewCreated(@NonNull View view, @Nullable Bundle savedInstanceState) {
        super.onViewCreated(view, savedInstanceState);
        View contentView = binding.getRoot().findViewById(R.id.refreshLayout);
        loadService = LoadSir.getDefault().register(contentView, new Callback.OnReloadListener() {
            @Override
            public void onReload(View v) {
                loadService.showCallback(LoadingCallback.class);
                goodsViewModel.isRefresh = true;
                goodsViewModel.productList();
            }
        });
        loadService.showCallback(LoadingCallback.class);
        goodsViewModel.productType();
    }

    @Override
    public void initView() {
        super.initView();
        binding.setMGoodsViewModel(goodsViewModel);
        //分类
        goodsPointsAdapter = new GoodsPointsAdapter();
        goodsPointsAdapter.setAddItemOnClickListener(new AddItemOnClickListener() {
            @Override
            public void onSkipClicked(int position, Object mObject) {
                MallPointsClass mallPointsClass = (MallPointsClass) mObject;
                if(null != mallPointsClass){
                    goodsViewModel.tabId.set(mallPointsClass.getId());
                    goodsViewModel.isRefresh = true;
                    goodsViewModel.productList();
                }
            }
        });
        binding.rvGoodsClassification.setLayoutManager(new LinearLayoutManager(mContext));
        binding.rvGoodsClassification.setAdapter(goodsPointsAdapter);
        //商品
        goodsAdapter = new GoodsAdapter();
        goodsAdapter.setAddItemOnClickListener(new AddItemOnClickListener() {
            @Override
            public void onSkipClicked(int position, Object mObject) {
                MallGoods mallGoods = (MallGoods) mObject;
                if(null != mallGoods){
                    Intent intent =new Intent(mContext, GoodsDetailsActivity.class);
                    intent.putExtra("mallGoods",mallGoods);
                    intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
                    startActivity(intent);//启动Activity
                }
            }
        });
        goodsAdapter.setAddItemOnClickListener(new AddOnClickListener() {
            @Override
            public void onSkipClicked(String sKip) {
                goodsViewModel.keywords.set(sKip);
                goodsViewModel.isRefresh = true;
                goodsViewModel.productList();
            }
        });
        gridLayoutManager = new GridLayoutManager(mContext,2);
        gridLayoutManager.setSpanSizeLookup(new GridLayoutManager.SpanSizeLookup() {
            @Override
            public int getSpanSize(int position) {
                if (position == 0){
                    return 2;
                }else{
                    return 1;
                }
            }
        });
        binding.rvGoods.addItemDecoration(new DividerItemDecoration(mContext));
        binding.rvGoods.setLayoutManager(gridLayoutManager);
        binding.rvGoods.setAdapter(goodsAdapter);
        //刷新
        binding.refreshLayout.setRefreshHeader(new ClassicsHeader(mContext));
        binding.refreshLayout.setRefreshFooter(new ClassicsFooter(mContext));
    }

    @Override
    public void initData() {
        super.initData();
        //分类
        goodsViewModel.listMutableLiveData.observe(this, new Observer<List<MallPointsClass>>() {
            @Override
            public void onChanged(List<MallPointsClass> mallPointsClasses) {
                if(null != mallPointsClasses && mallPointsClasses.size() > 0){
                    goodsViewModel.tabId.set(mallPointsClasses.get(0).getId());
                    mallPointsClasses.get(0).setSelected(true);
                    goodsPointsAdapter.setAdd(mallPointsClasses);
                    loadService.showSuccess();
                    goodsViewModel.isRefresh = true;
                    goodsViewModel.productList();
                }else {
                    loadService.showCallback(EmptyCallback.class);
                }
            }
        });
        goodsViewModel.mMallGoodsList.observe(this, new Observer<List<MallGoods>>() {
            @Override
            public void onChanged(List<MallGoods> mallGoods) {
                binding.refreshLayout.finishRefresh();
                if( goodsViewModel.isRefresh){
                    mallGoods.add(0,null);
                    goodsAdapter.setAdd(mallGoods);
                    goodsViewModel.keywords.set("");
                    if(mallGoods.size() > 1){
                        loadService.showSuccess();
                    }else {
                        loadService.showCallback(EmptyCallback.class);
                    }
                }else {
                    if(mallGoods.size() > 0){
                        goodsAdapter.ADD(mallGoods);
                        binding.refreshLayout.finishLoadMore();
                    }else {
                        binding.refreshLayout.finishLoadMoreWithNoMoreData();
                    }

                }
            }
        });
    }

    @Override
    public void initViewObservable() {
        super.initViewObservable();
        //刷新
        binding.refreshLayout.setOnRefreshListener(this);
        //加载
        binding.refreshLayout.setOnLoadMoreListener(this);
        //购物车
        binding.imgShoppingCar.setOnClickListener(new DrawableLayout.OnClickListener() {
            @Override
            public void onClick() {
                ARouter.getInstance().build(ARouterUtils.ShopingCarActivity)
                        .addFlags(Intent.FLAG_ACTIVITY_NEW_TASK)
                        .navigation();
            }
        });
    }

    @Override
    public void onRefresh(@NonNull @NotNull RefreshLayout refreshLayout) {
        goodsViewModel.isRefresh = true;
        goodsViewModel.productList();
    }

    @Override
    public void onLoadMore(@NonNull @NotNull RefreshLayout refreshLayout) {
        goodsViewModel.isRefresh = false;
        goodsViewModel.productList();
    }

    @Override
    public void onClick(View view) {
        switch (view.getId()){

        }
    }
}
